<?php
function get_lishihuijuece_by_id($id){
    $query = "SELECT * FROM {lishihuijuece_verdict} WHERE shenbaoqiye_id = ?";
    $query_result = db_query($query,array($id))->fetchAssoc();
    if($query_result == FALSE){
        db_insert('lishihuijuece_verdict')->fields(array('shenbaoqiye_id'=>$id))->execute();
        return "";
    }
    return $query_result['zongheyijian'];
}

function lishihuijuece_page(){
    $html = "";
    module_load_include('inc',"enterprise_information2","enterprise_information2.pages");
    $applyfunds_info = get_part_by_state(7);
    $html .= "<table style='width: 800px'><tr><th>基金名称</th><th>综合意见</th><th>操作</th></tr>";
    $count = 0;
    foreach($applyfunds_info as $fund_info){
        $count += 1;
        $html .= '<tr><td>'.$fund_info['nishejijinmingcheng'].'</td>';
        $html .= '<td>'.get_lishihuijuece_by_id($fund_info['id']).'</td>';
        $html .= '<td>'.l('填写意见','lishihuijuece/edit/'.$fund_info['id']).'</td></tr>';
    }
    if ($count == 0){
        $html = "当前没有需要理事会决策基金";
        return $html;
    }
    $html .= '</table>';
    
    $form = drupal_get_form("lishihuijuece_form");
    $html .= drupal_render($form);
    return $html;
}

function lishihuijuece_form(){
    $form = array();
    $form['upload'] = array(
        '#type' => 'managed_file',
        '#title' => '上传理事会汇报稿',
        '#upload_location' => 'public://my_uploads',
        '#upload_validators' => array(
            'file_validate_extensions' => array('doc xls pdf docx xlsx zip tar.gz 7z'),
            'file_validate_size' => array(50*1024*1024)),
    );
    $form['file_name'] = array(
        '#title' => t('显示文件名：'),
        '#type' => 'textfield',
        '#default_value' =>"附件",
    );
    $form['upload1'] = array(
        '#type' => 'managed_file',
        '#title' => '上传理事会决策结果文件',
        '#upload_location' => 'public://my_uploads',
        '#upload_validators' => array(
            'file_validate_extensions' => array('doc xls pdf docx xlsx zip tar.gz 7z'),
            'file_validate_size' => array(50*1024*1024)),
    );
    $form['file_name1'] = array(
        '#title' => t('显示文件名：'),
        '#type' => 'textfield',
        '#default_value' =>"附件",
    );
	module_load_include("inc","enterprise_information2",'enterprise_information2.pages');
	$applyfunds_info = get_part_by_state(7);
	$header = array(
        'jijinmingcheng' => t('基金名称'),
    );
	$options = array();
	foreach ($applyfunds_info as $fund_info) {
		$options[$fund_info['id']] = array(
			'jijinmingcheng' => l($fund_info['nishejijinmingcheng'],'enterprise_information2/viewbyid/'.$fund_info['id']),
		);
	}
	$form['table'] = array(
		'#type' => 'tableselect',
		'#header' => $header,
		'#options' => $options,
		'#empty' => t('没有基金记录'),
	);
    $form['save'] = array(
        '#type' => 'submit',
        '#value' => '暂存',
        '#submit' => array('lishihuijuece_form_save'),
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('提交'),
    );
    return $form;
}

function lishihuijuece_form_save($form,&$form_state){
	$values = $form_state['values'];
	$fid = $form_state['values']['upload']; 
	$file = file_load($fid);  
	$filename = $values['file_name'];
	if($filename == "")
		$filename = "附件";
	if ($file != null) {
		$saveurl = file_uri_target($file->uri);
	    $fid = $file->fid;
		$extension_errors=file_validate_extensions($file, "doc xls pdf docx xlsx zip tar.gz 7z");   
		if (!$extension_errors) {  
			$file->status = FILE_STATUS_PERMANENT;   
			file_save($file);
		}
		else {
			form_set_error('upload',$extension_errors[0]);
		}
	}
    if($file != NULL){
		$db_fields['file_name'] = $filename;
		$db_fields['url'] = $saveurl;
		$db_fields['file_managed_id'] = $fid;
	}
    $fid1 = $form_state['values']['upload1']; 
	$file1 = file_load($fid1);  
	$filename1 = $values['file_name1'];
	if($filename1 == "")
		$filename1 = "附件";
	if ($file1 != null) {
		$saveurl1 = file_uri_target($file1->uri);
	    $fid1 = $file1->fid;
		$extension_errors=file_validate_extensions($file, "doc xls pdf docx xlsx zip tar.gz 7z");   
		if (!$extension_errors) {  
			$file1->status = FILE_STATUS_PERMANENT;   
			file_save($file1);
		}
		else {
			form_set_error('upload',$extension_errors[0]);
		}
	}
    if($file != NULL){
		$db_fields['file_name1'] = $filename1;
		$db_fields['url1'] = $saveurl1;
		$db_fields['file_managed_id1'] = $fid1;
	}
    $query = "SELECT id FROM {pici} WHERE state = ?";
    $query_result = db_query($query,array('state'=>4))->fetchAssoc();
    $pici_id = $query_result['id'];
    $query = "SELECT * FROM {lishihuijuece_file} WHERE pici_id = ?";
    $query_result = db_query($query,array('pici_id'=>$pici_id))->fetchAssoc();
    if($query_result == FALSE){
        $db_fields['pici_id'] = $pici_id;
        db_insert('lishihuijuece_file')->fields($db_fields)->execute();
    } else {
        db_update('lishihuijuece_file')->fields($db_fields)->condition('pici_id',$pici_id)->execute();
    }
    $form_state['redirect'] = 'lishihuijuece'; 
}

function lishihuijuece_form_submit($form,&$form_state){
	$values = $form_state['values'];
	$fid = $form_state['values']['upload']; 
	$file = file_load($fid);  
	$filename = $values['file_name'];
	if($filename == "")
		$filename = "附件";
	if ($file != null) {
		$saveurl = file_uri_target($file->uri);
	    $fid = $file->fid;
		$extension_errors=file_validate_extensions($file, "doc xls pdf docx xlsx zip tar.gz 7z");   
		if (!$extension_errors) {  
			$file->status = FILE_STATUS_PERMANENT;   
			file_save($file);
		}
		else {
			form_set_error('upload',$extension_errors[0]);
		}
	}
    if($file != NULL){
		$db_fields['file_name'] = $filename;
		$db_fields['url'] = $saveurl;
		$db_fields['file_managed_id'] = $fid;
	}
    $fid1 = $form_state['values']['upload1']; 
	$file1 = file_load($fid1);  
	$filename1 = $values['file_name1'];
	if($filename1 == "")
		$filename1 = "附件";
	if ($file1 != null) {
		$saveurl1 = file_uri_target($file1->uri);
	    $fid1 = $file1->fid;
		$extension_errors=file_validate_extensions($file, "doc xls pdf docx xlsx zip tar.gz 7z");   
		if (!$extension_errors) {  
			$file1->status = FILE_STATUS_PERMANENT;   
			file_save($file1);
		}
		else {
			form_set_error('upload',$extension_errors[0]);
		}
	}
    if($file != NULL){
		$db_fields['file_name1'] = $filename1;
		$db_fields['url1'] = $saveurl1;
		$db_fields['file_managed_id1'] = $fid1;
	}
    $query = "SELECT id FROM {pici} WHERE state = ?";
    $query_result = db_query($query,array(4))->fetchAssoc();
    $pici_id = $query_result['id'];
    $query = "SELECT * FROM {lishihuijuece_file} WHERE pici_id = ?";
    $query_result = db_query($query,array($pici_id))->fetchAssoc();
    if($query_result == FALSE){
        $db_fields['pici_id'] = $pici_id;
        db_insert('lishihuijuece_file')->fields($db_fields)->execute();
    } else {
        db_update('lishihuijuece_file')->fields($db_fields)->condition('pici_id',$pici_id)->execute();
    }
    db_update('pici')->fields(array('state'=>5))->execute();
	module_load_include("inc","enterprise_information2",'enterprise_information2.pages');
	$funds_list = array();
	foreach ($values['table'] as $rows) {
		$funds_list[] = $rows;
	}
	$funds_array = get_part_by_state(7);
	foreach ($funds_array as $fund) {
		if(in_array($fund['id'], $funds_list, TRUE)&&$funds_list != NULL){
			set_state_by_id(9, $fund['id']);
		}else{
			set_state_by_id(8,$fund['id']);
		}
	}
    $form_state['redirect'] = 'lishihuijuece';
}

function lishihuijuece_edit_page($id){
    $html = "";
    $form = drupal_get_form('lishihuijuece_edit_form',$id);
    $html .= drupal_render($form);
    return $html;
}

function lishihuijuece_edit_form($form,&$form_state,$id){
    $form = array();
    $form['bid'] = array(
        '#type' => "hidden",
        "#value" => $id,
    );
    $form['zongheyijian'] = array(
        "#type" => "textarea",
        "#title" => "综合意见",
        "#default_value" => get_lishihuijuece_by_id($id),
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => '提交',
    );
    return $form;
}
function lishihuijuece_edit_form_submit($form,&$form_state){
	$values = $form_state['values'];
    $field_data = array();
    $field_data['zongheyijian'] = $values['zongheyijian'];
    $db_result = db_update('lishihuijuece_verdict')
    ->fields($field_data)
    ->condition('shenbaoqiye_id',$values['bid'])
    ->execute();
    if($db_result != NULL){
        $form_state['redirect'] = 'lishihuijuece';
    }else{
        drupal_set_message('提交出现问题');
    }
}